/*
 * Alloc.h
 *
 *  Created on: 2011-7-6
 *      Author: apple
 */
/*
 * Garbage-collecting allocator
 */
#ifndef ALLOC_H_
#define ALLOC_H_
#include <stdbool.h>
/*
 * Initialization.
 */
bool dvmGcStartup(void);
void dvmGcShutdown(void);

/*
 * Basic allocation function.
 *
 * The new object will be added to the "tracked alloc" table unless
 * flags is ALLOC_DONT_TRACK or ALLOC_NO_GC.
 *
 * Returns NULL and throws an exception on failure.
 */
void* dvmMalloc(size_t size, int flags);

//flags for dvmMalloc
enum {
	ALLOC_DEFAULT = 0x00,
	ALLOC_NO_GC = 0x01, /* do not garbage collect this object */
	ALLOC_DONT_TRACK = 0x02, /* don't add to internal tracking list */
	ALLOC_FINALIZABLE = 0x04,/* call finalize() before freeing */
};

/*
 * Allocate a new object.
 *
 * The new object will be added to the "tracked alloc" table unless
 * flags is ALLOC_DONT_TRACK or ALLOC_NO_GC.
 *
 * Returns NULL and throws an exception on failure.
 */
Object* dvmAllocObject(ClassObject* clazz, int flags);

/*
 * Track an object reference that is currently only visible internally.
 * This is called automatically by dvmMalloc() unless ALLOC_DONT_TRACK
 * is set.
 *
 * The "self" argument is allowed as an optimization; it may be NULL.
 */
void dvmAddTrackedAlloc(Object* obj, Thread* self);

/*
 * Remove an object from the internal tracking list.
 *
 * Does nothing if "obj" is NULL.
 *
 * The "self" argument is allowed as an optimization; it may be NULL.
 */
void dvmReleaseTrackedAlloc(Object* obj, Thread* self);
#endif /* ALLOC_H_ */
